import {getGeologys} from  "@/api/request"
import hashMap from "@/utils/hashMap"
import markIcon from "./mark.png"
import {BillboardGraphics,LabelGraphics,Color,HeightReference,Cartesian3,Cartesian2,NearFarScalar,VerticalOrigin,ScreenSpaceEventHandler,ScreenSpaceEventType,Cesium,defined,EntityCollection} from "cesium"
import infoBox from '@/plugins/toolsManage/infoWindow/infoWindow'
const geologyEntity = function(viewer,pram){
    this._viewer = viewer
    this.entityIds = new hashMap()
    this.infoWindow = new infoBox(viewer)
    this.infoWindowBtnClick = pram.infoWindowBtnClick || function(evt){
        console.log(evt)
    }
    this.entityCollection = new EntityCollection()
    this._bind_evt()
    // this.etityGroupName = pram.etityGroupName || "entitys1"
}
geologyEntity.prototype = {
    getData:function(){
        var self = this
        this.clear()
        getGeologys().then(res=>{
            try{
                console.log(res.data.rows)
                res.data.rows.forEach(geologyE => {
                    var lon = parseFloat(geologyE.location.split(" ")[1])
                    var lat = parseFloat(geologyE.location.split(" ")[0])
                    if(lon > 0  && lon <180 && lat>0 && lat<90){
                        var geologyId = geologyE.id || "0001"
                        var entity = {
                            position:Cartesian3.fromDegrees(lon, lat),
                            billboard:new BillboardGraphics({
                                image:markIcon,
                                // disableDepthTestDistance:Number.POSITIVE_INFINITY,
                                disableDepthTestDistance:180000,
                                heightReference:HeightReference.CLAMP_TO_GROUND,
                                pixelOffset:new Cartesian2(0.0, -18.0),
                                scale:1
                            }),
                            label:new LabelGraphics({
                                text:geologyE.name,
                                font : '12px sans-serif',
                                fillColor :new Color(1, 1, 1, 1),
                                outlineWidth : 2,
                                showBackground:true,
                                backgroundColor:new Color(0.1, 0.1, 0.1, 0.8),
                                verticalOrigin : VerticalOrigin.BOTTOM,
                                pixelOffset : new Cartesian2(0, 20),
                                scaleByDistance: new NearFarScalar(880000, 1, 5000000, 0),
                                // disableDepthTestDistance:Number.POSITIVE_INFINITY,
                                disableDepthTestDistance:Number.POSITIVE_INFINITY,
                                heightReference:HeightReference.CLAMP_TO_GROUND
                            }),
                            key:"geologyEntity",
                            id:geologyId,
                            click:function(){
                                var tempPoup = `<div class="infolist"><span >责任人:${geologyE.person}</span>
                                <span >时间:${geologyE.createTime}</span></br>
                                <span >电话:${geologyE.telephone}</span></br>
                                <span>描述:${geologyE.content}</span></div>
                                <div class="button" id="gotoEntity">实时监测</div>
                                `
                                self.infoWindow.show(geologyE.name,tempPoup)
                                $("#gotoEntity").click(function(){
                                    self.infoWindowBtnClick(geologyE)
                                })
                            }
                       }
                       self.add(entity)
                       
                    }else{
                        console.log("坐标不正确")
                    }
                });
                
            }catch{

            }
           
        })
    },
    _bind_evt:function(){
        var self  = this
        this.handler = new ScreenSpaceEventHandler(this._viewer.scene.canvas);
        this.handler.setInputAction(function (movement) {
            var pick = self._viewer.scene.pick(movement.position);
            if (defined(pick) && pick.id) {
                if(pick.id.click){
                    pick.id.click()
                }
            }else{
                self.infoWindow.close()
            }
        },ScreenSpaceEventType.LEFT_CLICK)
    },
    clear:function(){
        this.entityCollection.values.forEach((entity,index)=>{
            this._viewer.entities.remove(entity)
            if(index == this.entityCollection.values.length-1){
                this.entityCollection.removeAll()
            }
        })
    },
    add:function(entity){
        this.entityCollection.add(entity)
        this._viewer.entities.add(entity)
    },
    hide:function(){

    },
    show:function(){

    },
    addTo:function(){

    },
    goto:function(id){
        var currentEntity = this._viewer.entities.getById(id)
        if(currentEntity){
            this._viewer.flyTo(currentEntity)
        }
        
    }
}
export default geologyEntity